package test.tcp.one2many;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;

class ServerThread extends Thread {
	Socket socket;

	public ServerThread(Socket socket) {
		this.socket = socket;
	}

	public void run() {
		try {
			// 输入输出流
			BufferedReader is = new BufferedReader(new InputStreamReader(socket
					.getInputStream()));
			PrintWriter os = new PrintWriter(socket.getOutputStream());

			// 循环处理客户端输入
			String line;
			while ((line = is.readLine()) != null) {
				System.out.println("收到：" + line);
				if (line.equals("bye")) {
					break;
				} else {
					// 回复客户端
					os.println(line);
					os.flush();
				}
			}

			// 关闭输入输出流
			is.close();
			os.close();

			// 关闭客户端
			socket.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
}
